<?php

	
	
	class CaidanAction extends CommonAction{
	
	
	 
	   
	     public function sc(){
	   
	   
	     header('Content-Type: text/html; charset=UTF-8');

//更换成自己的APPID和APPSECRET

$wx=M('wx')->where('id=1')->find();
$APPID=$wx['appid'];
$APPSECRET=$wx['appsecret'];





if(time()>$mytoken['time']){

$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;

$json=file_get_contents($TOKEN_URL);
$result=json_decode($json);


$ACC_TOKEN=$result->access_token;

$st['token']=$ACC_TOKEN;
$st['time']=time()+3600;
M('token')->where('id=1')->save($st);

}else{

$ACC_TOKEN=$mytoken['token'];

}

//var_dump($ACC_TOKEN);


$ar=M('caidan')->where('topid=0')->select();




//$arr=array("button"=>array('0'=>array('type'=>"view","name"=>"2222","url"=>"http://baidu.com")));

//$arr1=json_encode($arr);

//var_dump($arr1);exit();

$data=$this->test();


$MENU_URL="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$ACC_TOKEN;



$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $MENU_URL); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$info = curl_exec($ch);

if (curl_errno($ch)) {
	echo 'Errno'.curl_error($ch);
}

curl_close($ch);

//var_dump($info);
	   
	   $re=json_decode($info);
	   
	   //var_dump($re);
	   
	   $ajax=($re->errcode==0)?1:2;
	   
	   $this->ajaxReturn($ajax);
	   
	   
	   
	   }
	   
	   
	 
	   //$arr=array("button"=>array('0'=>array('type'=>"view","name"=>"2222","url"=>"http://baidu.com")));
	   
	   public function test(){
	   
	   
	      
             $ar=M('nav')->where('cid=0')->order('sort desc')->select();
			 
			
			 
			   $i=0;
			 foreach($ar as $v){
			   
			   $re=M('nav')->where('cid="%s"',$v['id'])->find();
			   
			  
			 
			   if($re==null){
			   
			     
			      if($v['type']=="链接"){
				 
				  $arr[$i]=array("type"=>"view","name"=>$v['title'],"url"=>$v['url']);
				  
				  }
				
				  if($v['type']=="选择照片"){
				  
				   $arr[$i]=array("type"=>"pic_photo_or_album","name"=>$v['title'],"key"=>'rselfmenu_1_1',"sub_button"=>[ ]);
				  
				  }
				  
				  if($v['type']=="回复内容"){
				  
				   $arr[$i]=array("type"=>"click","name"=>$v['title'],"key"=>$v['id']);
				  
				  }
				  
				 
				  
			   }else{
			   
					
					$arr[$i]['name']=$v['title'];
					
					
					$down=M('nav')->where('cid="%s"',$v['id'])->select();
					 
					// dump($down);
				
					foreach($down as $vv){
					
					  if($vv['type']=="链接"){
					    $d[]=array("type"=>"view","name"=>$vv['title'],"url"=>$vv['url']);
					  }
					  
					   if($vv['type']=="选择照片"){
				  
				        $d[]=array("type"=>"pic_photo_or_album","name"=>$vv['title'],"key"=>'rselfmenu_1_1',"sub_button"=>[ ]);
				  
				      }
				  
				       if($vv['type']=="回复内容"){
				  
				        $d[]=array("type"=>"click","name"=>$vv['title'],"key"=>$vv['id']);
				  
				     }
					
					}
					
					$arr[$i]['sub_button']=$d;
			   
			        unset($d);
			   }
			   
			   $i++;
			 }

             //dump($arr);
			 $data['button']=$arr;
			 
			// dump($data);
			 $json=json_encode($data,JSON_UNESCAPED_UNICODE);
			 
			 //dump($json);
			
			 
			 return $json;
	   
	   }
	
	
	
	
	
	}